#ifndef __SLOTHTTPD_CONFIG_FORMATTER_HPP__
#define __SLOTHTTPD_CONFIG_FORMATTER_HPP__

#include <slothttpd/types.hpp>
#include <slothttpd/http/request.hpp>
#include <slothttpd/config/location/base_location.hpp>

#include <vector>
#include <string>

#include <boost/function.hpp>
#include <boost/range/algorithm/copy.hpp>

namespace slothttpd {
namespace config {

class formatter
{
public:
    static const char escape;
    
    formatter(const std::string &format, base_location& loc);
    
    std::size_t place(char_range_t &dest, http::request &req) const;
    std::size_t size(http::request &req) const;
    
private:
    template<typename TRange>
    std::size_t place_range(char_range_t &dest, TRange src)
    {
        if (std::size_t(src.size()) <= std::size_t(dest.size())) {
            boost::range::copy(src, dest.begin());
            dest.advance_begin(src.size());
            
            return 0;
        }
        
        return src.size();
    }
    
    void push_constant_action(const std::string &str);
    
private:
    typedef boost::function<std::size_t(char_range_t&, http::request&)> part_string_type;
    
    std::vector<part_string_type> actions_;
};

}
}

#endif
